$(function() {
	$('.slider1').bxSlider({
		auto : true,
		adaptiveHeight: true,
		startSlides: 0, 
        slideMargin: 3
	});
	
	$('.con_detail img').each(function(i, img) {
		$(this).removeAttr("style");
	});
	
	loadCarCount();//加载购物车数量
	
	/**
	 * 数量减
	 */
	$('.c_subtract').click(function(){
		var count = parseInt($('.c_number input').val());
		if(count > 1){
			$('.c_number input').val((count-1));
		}
	});
	/**
	 * 数量加
	 */
	$('.c_add').click(function(){
		var count = parseInt($('.c_number input').val());
		var stock = parseInt($('#stock').text());
		if(stock > count){
			$('.c_number input').val((count+1));
		}else{
			$.toastWeb({message:'库存不足'});
		}
	});
	/**
	 * 加载购物车事件
	 */
	$('#addCar').click(function(){
		var stock = parseInt($('#stock').text());
		var count = parseInt($('#chose-count').val());
		var productId = $('#productId').val();
		if(stock == 0 || count > stock){
			$.toastWeb({message:'库存不足'});
			return;
		}
		var mask;
		$.ajax({
		   type: "POST",
		   async:false,
		   cache:false,
		   dataType:'json',
		   url: ctx + '/web/car/addProduct',
		   beforeSend: function(){
			   mask = $.maskWeb();
		   },
		   data: {'count':count,'productId':productId,'openid':$('#openid').val()},
		   success: function(data){
			   if(data.success){
				   loadCarCount();
			   }else{
				   $.toastWeb({message:data.message});
			   }
		   },
		   complete:function(){
			   $.unmaskWeb(mask);  
		   }
		});
	});
	
	/**
	 * 购物车
	 */
	$('#cars').click(function(){
		/*var carNumber = parseInt($('#car-number').text());
		if(isNaN(carNumber)){
			carNumber = 0;
		}
		if(carNumber>0){*/
			location.href = ctx + '/web/weixin/preSaleOrder/cars';
		/*}else{
			$.toastWeb({message:'购物车空空的'});
		}*/
	});
	
	/**
	 * 立即下单
	 */
	$('#addOrder').click(function(){
		var stock = parseInt($('#stock').text());
		var count = parseInt($('#chose-count').val());
		var productId = $('#productId').val();
		if(stock == 0 || count > stock){
			$.toastWeb({message:'库存不足'});
			return;
		}
		var mask;
		$.ajax({
		   type:"POST",
		   async:false,
		   cache:false,
		   dataType:'json',
		   url: ctx + '/web/car/addProduct',
		   beforeSend: function(){
			   mask = $.maskWeb();
		   },
		   data: {'count':count,'productId':productId,'openid':$('#openid').val()},
		   success: function(data){
			   if(data.success){
				    var array = [];
					var obj = new Object();
					obj.id = productId;
					obj.count = count;
					array.push(obj);
					var jsonText = JSON.stringify(array);
					$('#goods').val(jsonText);
					orderForm.submit();
			   }else{
				   $.toastWeb({message:data.message});
			   }
		   },
		   complete:function(){
			   $.unmaskWeb(mask);  
		   }
		});
	});
});

/**
 * 加载购物车数量
 */
function loadCarCount(){
	var mask;
	$.ajax({
	   type: "POST",
	   async:false,
	   cache:false,
	   dataType:'json',
	   url: ctx + '/web/car/detail',
	   data: {openid:$('#openid').val()},
	   beforeSend: function(){
		   mask = $.maskWeb();
	   },
	   success: function(data){
		   if(data.success){
			  var car = data.data;
			  $('#car-number').text(car.productCount);
			  if(car.productCount > 0){
				  $('.number-bg').show();
			  }else{
				  $('.number-bg').hide();
			  }
		   }else{
			   $.toastWeb({message:data.message});
		   }
	   },
	   complete:function(){
		   $.unmaskWeb(mask);
	   }
	});
}